package com.example.deliveryserver.controller;

import com.example.deliveryserver.entities.DeliveryAddress;
import com.example.deliveryserver.service.DeliveryAddressService;
import com.example.server_engineer.controller.AbstractTypedController;
import com.example.server_engineer.controller.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;


@RestController
@Tag(name = "配送地址控制器")
@RequestMapping("delivery_address")
public class DeliveryAddressController
        extends AbstractTypedController<DeliveryAddress, Integer> {

    DeliveryAddressController(DeliveryAddressService deliveryAddressService){
        this.svcContext = deliveryAddressService;
    }
    @Resource
    DeliveryAddressService dsvc;

    @Operation(summary = "根据用户id查询地址信息")
    @GetMapping("/query/byuser")
    public DeliveryAddress queryAddressByUserid(String userid){
        return dsvc.queryAddress(userid);
    }

    @Operation(summary = "根据id查询地址信息")
    @GetMapping("/query/by/{id}")
    public R queryAddressById(@PathVariable Integer id){
        Optional<DeliveryAddress> res =  dsvc.queryAddressById(id);
        return res.map(deliveryAddress -> new R(true, deliveryAddress)).orElseGet(() -> new R(false, null));
    }
}
